package com.hhit.zhgd.config

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.hhit.zhgd.domain.entity.ZhgdDictionary
import com.hhit.zhgd.service.ZhgdDictionaryService
import jakarta.annotation.Resource
import jakarta.servlet.ServletContext
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.InitializingBean
import org.springframework.context.annotation.Configuration
import org.springframework.web.context.ServletContextAware
import java.util.concurrent.ConcurrentHashMap

@Configuration
class InitDictionaryConfig : InitializingBean, ServletContextAware {
    private val log = LoggerFactory.getLogger(InitDictionaryConfig::class.java)

    companion object {
        var DICTIONARY_MAP: MutableMap<String, String?> = ConcurrentHashMap()

        var PROJECT_PLAN_STATUS = "project_plan_status"

        var WORK_TICKET_STATUS = "work_ticket_status"

        var QUALITY_ACCEPTANCE_PLAN_STATUS = "quality_acceptance_plan_status"

        var HAND_IN_DRAWINGS_FLAG = "hand_in_drawings_flag"

        var WITNESS_OR_NOT_FLAG = "witness_or_not_flag"

        var NOTICE_TYPE = "notice_type"
    }

    @Resource
    private val zhgdDictionaryService: ZhgdDictionaryService? = null

    @Throws(Exception::class)
    override fun afterPropertiesSet() {
        val parentCodes = zhgdDictionaryService!!.list(
            QueryWrapper<ZhgdDictionary?>().eq("parent_code", "").or().isNull("parent_code")
        )
        for (zhgdDictionary in parentCodes) {
            val codes = zhgdDictionaryService.list(
                QueryWrapper<ZhgdDictionary?>().eq("parent_code", zhgdDictionary.code)
            )
            for (child in codes) {
                DICTIONARY_MAP[String.format("%s/%s", zhgdDictionary.code, child.code)] = child.value
            }
        }
        log.info("InitDictionaryConfig==>DICTIONARY_MAP:{}", DICTIONARY_MAP)
    }

    override fun setServletContext(servletContext: ServletContext) {

    }
}